package jedis;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class jedisTest {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("8.130.9.11", 6379);
        // jedis.auth("要输入的密码");
        try {
            jedis.select(1);
            jedis.set("sn", "789");
            String sn = jedis.get("sn");
            System.out.println(sn);
            System.out.println("============存储和获取redis上的数据=======================");
            jedis.mset(new String[]{"name", "mingren", "sex", "男", "age", "20"});
            List<String> peoples = jedis.mget(new String[]{"name", "sex"});
            Long age = jedis.incr("age");
            System.out.println(age);
            System.out.println(peoples.toString());
            System.out.println("===============jedis操作hash类型数据=======================");
            jedis.hset("student:3312", "name", "鸣人");
            String name = jedis.hget("student:3312", "name");
            System.out.println(name);
            System.out.println("===============jedis存取Map类型的数据========================");
            Map<String, String> studentMap = new HashMap<>();
            studentMap.put("name", "Tom");
            studentMap.put("age", "10");
            studentMap.put("id", "3310");
            jedis.hmset("student:3310", studentMap);
            Map<String, String> sm = jedis.hgetAll("student:3310");
            System.out.println(sm.toString());
            System.out.println("==============jedis存取list类型的数据==========================");
            jedis.del("letter");
            jedis.rpush("letter", new String[]{"d", "e", "f"});
            jedis.lpush("letter", new String[]{"c", "b", "a"});
            jedis.lpop("letter");
            jedis.rpop("letter");
            //返回指定范围的元素
            List<String> letter = jedis.lrange("letter", 0, -1);
            System.out.println(letter.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }

    }
}
